package myfx.component;
import Globals.GongZuoMianGlobal;
import Globals.ZuanChangGlobal;
import Globals.ZuanKongGlobal;
import brain.MyBrain;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class TextFieldTreeCellImpl extends TreeCell<String>{
        
        final MyController controller;
        private TextField textField;
        private final ContextMenu contextMenu=new ContextMenu();
        private MyBrain brain;
        public TextFieldTreeCellImpl(final MyController controller,final MyBrain brain){
            
            this.controller=controller;
            this.brain=brain;
            final MenuItem addMenuItem=new MenuItem(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("添加"));
            final MenuItem delMenuItem=new MenuItem(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("删除"));
            final MenuItem editMenuItem=new MenuItem(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("详细编辑"));
            contextMenu.getItems().add(addMenuItem);
            contextMenu.getItems().add(delMenuItem);
            contextMenu.getItems().add(editMenuItem);
            addMenuItem.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {
                    if (getTreeItem().getParent()==null) {
//                        addMenuItem.setText("添加工作面");
                        addMenuItem.setDisable(false);
                        TreeItem<String> employeeItem=new TreeItem<String>(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("工作面")+(GongZuoMianGlobal.countI++));
                        getTreeItem().getChildren().add(employeeItem);
                    }else if(getTreeItem().getParent().getParent()==null){
//                        addMenuItem.setText("添加钻孔");
                        addMenuItem.setDisable(false);
                        TreeItem<String> employeeItem=new TreeItem<String>(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("X#钻场")+(ZuanChangGlobal.countI++));
                        getTreeItem().getChildren().add(employeeItem);
                    }else if(getTreeItem().getParent().getParent().getParent()==null){
                        addMenuItem.setDisable(false);
                        TreeItem<String> employeeItem=new TreeItem<String>(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("钻孔")+java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("X_")+ (ZuanKongGlobal.countI++));
                        getTreeItem().getChildren().add(employeeItem);
                    }else{
                        addMenuItem.setDisable(true);
                    }                  
                }
            });
            delMenuItem.setOnAction(new EventHandler<ActionEvent>() {

                public void handle(ActionEvent event) {
                    if (getTreeItem().getParent()==null) {
                        System.out.println(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("此处不能删除"));
                    }else if(getTreeItem().getParent().getParent()==null){
                        try {
                            getTreeItem().getParent().getChildren().remove(getTreeItem());
                            OperateMyDatabaseImpl.doDelete(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DELETE FROM GONGZUOMIAN;"));
                            OperateMyDatabaseImpl.doDelete(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DELETE FROM ZUANCHANG;"));
                            OperateMyDatabaseImpl.doDelete(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DELETE FROM ZUANKONG"));
                        } catch (Exception ex) {
                            Logger.getLogger(TextFieldTreeCellImpl.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }else if(getTreeItem().getParent().getParent().getParent()==null){
                        try {
                            getTreeItem().getParent().getChildren().remove(getTreeItem());
                            OperateMyDatabaseImpl.doDelete(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DELETE FROM ZUANCHANG WHERE MINGCHENG='")+getString()+java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("';"));
                            OperateMyDatabaseImpl.doDelete(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DELETE FROM ZUANKONG WHERE ZUANCHANG='")+getString()+java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("';"));
                        } catch (Exception ex) {
                            Logger.getLogger(TextFieldTreeCellImpl.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }else if(getTreeItem().getParent().getParent().getParent().getParent()==null){
                        try {
                            getTreeItem().getParent().getChildren().remove(getTreeItem());
                            OperateMyDatabaseImpl.doDelete(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("DELETE FROM ZUANKONG WHERE MINGCHENG='")+getString()+java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("';"));
                        } catch (Exception ex) {
                            Logger.getLogger(TextFieldTreeCellImpl.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            });
            editMenuItem.setOnAction(new EventHandler<ActionEvent>() {

                public void handle(ActionEvent t) {
//                    new MyDialog(null, false, "信息公告", "编辑跑了");
                    try{
                        if(getTreeItem().getParent()==null){
                            editMenuItem.setDisable(true);
                        }
                        else if(getTreeItem().getParent().getParent()==null){
                            editMenuItem.setDisable(false);
                            makeDialog_GongZuoMian();
                        }else if(getTreeItem().getParent().getParent().getParent()==null){
                            editMenuItem.setDisable(false);
                            makeDialog_ZuanChang();
                        }else if(getTreeItem().getParent().getParent().getParent().getParent()==null){
                            editMenuItem.setDisable(false);
                            makeDialog_ZuanKong();
                        }                       
                    }catch(Exception e){
                        
                    }
                    
                }
            });
        }
        @Override
        public void cancelEdit() {
            super.cancelEdit(); 
            setText(getItem().toString());
            setGraphic(getTreeItem().getGraphic());
        }

        @Override
        public void startEdit() {
            super.startEdit(); //To change body of generated methods, choose Tools | Templates.
            if(textField==null){
                createTextField();
            }
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty); 
            if(empty){
                setText(null);
                setGraphic(null);
            }else{
                if(isEditing()){
                    if(textField!=null){
                        textField.setText(getString());
                        setText(null);
                        setGraphic(textField);
                    }
                }else{
                    setText(getString());
                    setGraphic(getTreeItem().getGraphic());
//                    if(!getTreeItem().isLeaf()&&getTreeItem().getParent()!=null){
                        setContextMenu(contextMenu);
//                    }
                }
            }
        }
        public void createTextField(){
            textField=new TextField(getString());
            textField.setOnKeyReleased(new EventHandler<KeyEvent>() {

                public void handle(KeyEvent event) {
                    if(event.getCode()==KeyCode.ENTER){
                        commitEdit(textField.getText());
                    }else if(event.getCode()==KeyCode.ESCAPE)
                        cancelEdit();
                }
            });
        }
        public String getString(){
            return getItem()==null?"":getItem().toString();
        }
        public void makeDialog_ZuanKong()throws Exception{
            ZuanKong_Copy zkc=new ZuanKong_Copy(controller,getTreeItem().getValue(),this,brain, null, true, getTreeItem().getValue());
            zkc.show();
        }
        public void makeDialog_ZuanChang()throws Exception{
            ZuanChang_Copy zcc=new ZuanChang_Copy(controller,getTreeItem().getValue(),this,brain, null, true, getTreeItem().getValue());
            zcc.show();
        }
        public void makeDialog_GongZuoMian()throws Exception{
            //new Dialog_GongZuoMian(controller,getTreeItem().getValue(),this,brain);
            GongZuoMian_Copy gzmc=new GongZuoMian_Copy(controller,getTreeItem().getValue(),this,brain, null, true, getTreeItem().getValue());
            //gzmc.sizeToScene();
            gzmc.show();
        }
        public void makeDialog_CaiChang()throws Exception{
            
        }
        
    }
